package cn.com.scooper.schedue;

import cn.com.scooper.common.constant.CommonMineEnum;
import cn.com.scooper.pojo.po.MineBasicDataPo;
import cn.com.scooper.pojo.po.MineIotDevicePo;
import cn.com.scooper.pojo.po.MineIotIndexPo;
import cn.com.scooper.pojo.vo.IotDeviceInfoVo;
import cn.com.scooper.service.xf.MineBasicDataService;
import cn.com.scooper.service.xf.MineIotDeviceService;
import cn.com.scooper.utils.MineCityRequestUtils;
import cn.com.scooper.utils.MineDeviceCodeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.com.scooper.common.constant.CommonMineConstant.*;
import static cn.com.scooper.common.constant.CommonMineEnum.*;

/**
 * @author lijuntao
 * @version 1.0
 * @date 2023/11/8 11:12
 */
@Component
@EnableScheduling
public class MineDataScheduled {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private MineIotDeviceService mineIotDeviceService;
    @Autowired
    private MineCityRequestUtils mineCityRequestUtils;
    @Autowired
    private MineBasicDataService mineBasicDataService;

    private static final String HONG_SHI_MINE_ID = "635868160158863360";
    private static final Logger log = LoggerFactory.getLogger(MineDataScheduled.class);


    @Scheduled(cron = "0 0 0/6 * * ? ")
    public void refreshToken() {
        log.info("开始【刷新token】");
        mineCityRequestUtils.getJwtToken();
        log.info("结束【刷新token】");
    }

    /**
     * 红狮设备信息同步定时任务
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void hongShiDeviceScheduled() {
        log.info("进入【红狮】边坡位移监测设备定时同步数据");
        restTemplate.getMessageConverters().add(0,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String param = "mineAreaId=" + HONG_SHI_MINE_ID;
        String url = MINE_MONITOR_DEVICE_URL + "?" + param;
        String response;
        try {
            response = restTemplate.getForObject(url, String.class);
        } catch (RestClientException e) {
            log.error(e.toString());
            return;
        }

        if (StringUtils.isEmpty(response)) {
            log.info("【红狮】边坡位移监测设备接口请求失败：结果为空");
            return;
        }
//        response = new String(response.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (200 != jsonObject.getInteger("status") && !"ok".equals(jsonObject.getString("message"))) {
            log.info("【红狮】边坡位移监测设备接口请求异常：状态码为：" + jsonObject.getInteger("status"));
            return;
        }
        log.info("【红狮】边坡位移监测设备接口请求成功{}",response);

        //矿山边坡位移设备数据
        List<IotDeviceInfoVo> iotDeviceInfoVos = JSONArray.parseArray(jsonObject.getString("data"), IotDeviceInfoVo.class);
        //查询已存设备数据
        MineIotDevicePo mineIotDevicePo = new MineIotDevicePo();
        mineIotDevicePo.setCompanyCode(HONGSHI_COMPANY_CODE.getCode());
        mineIotDevicePo.setEquipType(CommonMineEnum.HZD770101.getCode());
        List<MineIotDevicePo> mineIotSlopeDeviceList = mineIotDeviceService.getMineIotDeviceList(mineIotDevicePo);

        // 处理新增、编辑和删除的数据
        processDeviceData(iotDeviceInfoVos, mineIotSlopeDeviceList, HONGSHI_COMPANY_CODE.getCode(), CommonMineEnum.HZD770101.getCode());
    }

    /**
     * 南方设备信息同步定时任务
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void nanFangDeviceScheduled() {
        log.info("进入【南方】监测设备定时同步数据");
        restTemplate.getMessageConverters().add(0,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String param = "mineAreaId=1";
        String url = NANFANG_MINE_MONITOR_DEVICE_URL + "?" + param;
        String response;
        try {
            response = restTemplate.getForObject(url, String.class);
        } catch (RestClientException e) {
            log.error(e.toString());
            return;
        }

        if (StringUtils.isEmpty(response)) {
            log.info("【南方】监测设备接口请求失败：结果为空");
            return;
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (200 != jsonObject.getInteger("status") && !"ok".equals(jsonObject.getString("message"))) {
            log.info("【南方】监测设备接口请求异常：状态码为：" + jsonObject.getInteger("status"));
            return;
        }
        log.info("【南方】边坡位移监测设备接口请求成功{}",response);

        //矿山设备数据
        List<IotDeviceInfoVo> iotDeviceInfoVos = JSONArray.parseArray(jsonObject.getString("data"), IotDeviceInfoVo.class);
        Map<Integer, List<IotDeviceInfoVo>> deviceMap = iotDeviceInfoVos.stream().collect(Collectors.groupingBy(IotDeviceInfoVo::getDeviceType));

        //查询已存设备数据
        MineIotDevicePo mineIotDevicePo = new MineIotDevicePo();
        mineIotDevicePo.setCompanyCode(NANFANG_COMPANY_CODE.getCode());
        List<MineIotDevicePo> mineIotSlopeDeviceList = mineIotDeviceService.getMineIotDeviceList(mineIotDevicePo);
        Map<String, List<MineIotDevicePo>> queryDeviceMap = mineIotSlopeDeviceList.stream().collect(Collectors.groupingBy(MineIotDevicePo::getEquipType));

        //设备类型1-边坡设备 2-雨量设备
        List<IotDeviceInfoVo> iotSlopeDeviceInfoVos = deviceMap.get(1);
        List<IotDeviceInfoVo> iotRainDeviceInfoVos = deviceMap.get(2);

        // 处理新增、编辑和删除的数据
        processDeviceData(iotSlopeDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770101.getCode()), NANFANG_COMPANY_CODE.getCode(), CommonMineEnum.HZD770101.getCode());
        processDeviceData(iotRainDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770102.getCode()), NANFANG_COMPANY_CODE.getCode(), CommonMineEnum.HZD770102.getCode());
    }

    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void changYuDeviceScheduled() {
//        String deviceData = "[{\"deviceType\":1,\"gdLng\":119.79141,\"deviceSerialNo\":\"TLJZ01\",\"level\":{\"red\":null,\"blue\":null,\"yellow\":null},\"gdLat\":29.79375,\"wgs84Lat\":29.79375,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":null,\"blue\":null,\"yellow\":null},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080101\",\"wgs84Lng\":119.79141,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.793182,\"deviceSerialNo\":\"JC01\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.795172,\"wgs84Lat\":29.795172,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080102\",\"wgs84Lng\":119.793182,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7938429733,\"deviceSerialNo\":\"JC02\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7944915283,\"wgs84Lat\":29.7944915283,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080103\",\"wgs84Lng\":119.7938429733,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.793648755,\"deviceSerialNo\":\"JC03\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793501205,\"wgs84Lat\":29.793501205,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080104\",\"wgs84Lng\":119.793648755,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.793229955,\"deviceSerialNo\":\"JC04\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79259078,\"wgs84Lat\":29.79259078,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080105\",\"wgs84Lng\":119.793229955,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.794085275,\"deviceSerialNo\":\"JC05\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.794799145,\"wgs84Lat\":29.794799145,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080106\",\"wgs84Lng\":119.794085275,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79395796,\"deviceSerialNo\":\"JC06\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080107\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"JC07\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080108\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7933889467,\"deviceSerialNo\":\"JC08\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7921069433,\"wgs84Lat\":29.7921069433,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080109\",\"wgs84Lng\":119.7933889467,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7937344483,\"deviceSerialNo\":\"JC09\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7924069583,\"wgs84Lat\":29.7924069583,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080110\",\"wgs84Lng\":119.7937344483,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7945844483,\"deviceSerialNo\":\"JC10\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7944669583,\"wgs84Lat\":29.7944669583,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080111\",\"wgs84Lng\":119.7945844483,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7944265617,\"deviceSerialNo\":\"JC11\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7932968717,\"wgs84Lat\":29.7932968717,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080112\",\"wgs84Lng\":119.7944265617,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79421,\"deviceSerialNo\":\"JC12\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79226,\"wgs84Lat\":29.79226,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080113\",\"wgs84Lng\":119.79421,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.795071305,\"deviceSerialNo\":\"JC13\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793259695,\"wgs84Lat\":29.793259695,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080114\",\"wgs84Lng\":119.795071305,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79243,\"deviceSerialNo\":\"JC14\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79077,\"wgs84Lat\":29.79077,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080116\",\"wgs84Lng\":119.79243,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79267,\"deviceSerialNo\":\"JC15\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.78941,\"wgs84Lat\":29.78941,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080115\",\"wgs84Lng\":119.79267,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79211677959087,\"deviceSerialNo\":\"JC16\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789688070266067,\"wgs84Lat\":29.789688070266067,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080117\",\"wgs84Lng\":119.79211677959087,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79227847458262,\"deviceSerialNo\":\"JC17\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789347203556893,\"wgs84Lat\":29.789347203556893,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080118\",\"wgs84Lng\":119.79227847458262,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79187423710326,\"deviceSerialNo\":\"JC18\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.790267932778004,\"wgs84Lat\":29.790267932778004,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080119\",\"wgs84Lng\":119.79187423710326,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79205838973274,\"deviceSerialNo\":\"JC19\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789652808247087,\"wgs84Lat\":29.789652808247087,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080120\",\"wgs84Lng\":119.79205838973274,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79208084737049,\"deviceSerialNo\":\"JC20\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.78918656481612,\"wgs84Lat\":29.78918656481612,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080121\",\"wgs84Lng\":119.79208084737049,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79153737253714,\"deviceSerialNo\":\"JC21\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.788649794451683,\"wgs84Lat\":29.788649794451683,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080122\",\"wgs84Lng\":119.79153737253714,\"deviceStatus\":0},{\"deviceType\":3,\"gdLng\":119.7935252333,\"deviceSerialNo\":\"JR01\",\"level\":{\"red\":550,\"blue\":450,\"yellow\":500},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"渗流压力\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080201\",\"wgs84Lng\":119.7935252333,\"deviceStatus\":0},{\"deviceType\":3,\"gdLng\":118.698284,\"deviceSerialNo\":\"JR02\",\"level\":{\"red\":550,\"blue\":450,\"yellow\":500},\"gdLat\":32.166114,\"wgs84Lat\":32.166114,\"deviceTypeName\":\"渗流压力\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080202\",\"wgs84Lng\":118.698284,\"deviceStatus\":0},{\"deviceType\":2,\"gdLng\":119.7934589467,\"deviceSerialNo\":\"TLYL01\",\"level\":{\"red\":100,\"blue\":50,\"yellow\":80},\"gdLat\":29.7921069433,\"wgs84Lat\":29.7921069433,\"deviceTypeName\":\"降雨量\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080401\",\"wgs84Lng\":119.7934589467,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW1-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080301\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080302\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080303\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080304\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080305\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080306\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080307\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080308\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080309\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080310\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080311\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080312\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080313\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080314\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080315\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080316\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":5,\"gdLng\":119.79142,\"deviceSerialNo\":\"JSD01\",\"level\":{\"red\":null,\"blue\":null,\"yellow\":null},\"gdLat\":29.79385,\"wgs84Lat\":29.79385,\"deviceTypeName\":\"振动加速度\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080501\",\"wgs84Lng\":119.79142,\"deviceStatus\":0},{\"deviceType\":5,\"gdLng\":119.79005,\"deviceSerialNo\":\"JSD02\",\"level\":{\"red\":null,\"blue\":null,\"yellow\":null},\"gdLat\":29.79139,\"wgs84Lat\":29.79139,\"deviceTypeName\":\"振动加速度\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080502\",\"wgs84Lng\":119.79005,\"deviceStatus\":0},{\"deviceType\":5,\"gdLng\":119.78927,\"deviceSerialNo\":\"JSD03\",\"level\":{\"red\":null,\"blue\":null,\"yellow\":null},\"gdLat\":29.78917,\"wgs84Lat\":29.78917,\"deviceTypeName\":\"振动加速度\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080503\",\"wgs84Lng\":119.78927,\"deviceStatus\":0}]";
        //这里的json去除了设备类型指标数据为空的情况
        String deviceData = "[{\"deviceType\":1,\"gdLng\":119.793182,\"deviceSerialNo\":\"JC01\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.795172,\"wgs84Lat\":29.795172,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080102\",\"wgs84Lng\":119.793182,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7938429733,\"deviceSerialNo\":\"JC02\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7944915283,\"wgs84Lat\":29.7944915283,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080103\",\"wgs84Lng\":119.7938429733,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.793648755,\"deviceSerialNo\":\"JC03\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793501205,\"wgs84Lat\":29.793501205,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080104\",\"wgs84Lng\":119.793648755,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.793229955,\"deviceSerialNo\":\"JC04\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79259078,\"wgs84Lat\":29.79259078,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080105\",\"wgs84Lng\":119.793229955,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.794085275,\"deviceSerialNo\":\"JC05\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.794799145,\"wgs84Lat\":29.794799145,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080106\",\"wgs84Lng\":119.794085275,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79395796,\"deviceSerialNo\":\"JC06\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080107\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"JC07\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080108\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7933889467,\"deviceSerialNo\":\"JC08\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7921069433,\"wgs84Lat\":29.7921069433,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080109\",\"wgs84Lng\":119.7933889467,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7937344483,\"deviceSerialNo\":\"JC09\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7924069583,\"wgs84Lat\":29.7924069583,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080110\",\"wgs84Lng\":119.7937344483,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7945844483,\"deviceSerialNo\":\"JC10\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7944669583,\"wgs84Lat\":29.7944669583,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080111\",\"wgs84Lng\":119.7945844483,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.7944265617,\"deviceSerialNo\":\"JC11\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.7932968717,\"wgs84Lat\":29.7932968717,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080112\",\"wgs84Lng\":119.7944265617,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79421,\"deviceSerialNo\":\"JC12\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79226,\"wgs84Lat\":29.79226,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080113\",\"wgs84Lng\":119.79421,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.795071305,\"deviceSerialNo\":\"JC13\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.793259695,\"wgs84Lat\":29.793259695,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080114\",\"wgs84Lng\":119.795071305,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79243,\"deviceSerialNo\":\"JC14\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.79077,\"wgs84Lat\":29.79077,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080116\",\"wgs84Lng\":119.79243,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79267,\"deviceSerialNo\":\"JC15\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.78941,\"wgs84Lat\":29.78941,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080115\",\"wgs84Lng\":119.79267,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79211677959087,\"deviceSerialNo\":\"JC16\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789688070266067,\"wgs84Lat\":29.789688070266067,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080117\",\"wgs84Lng\":119.79211677959087,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79227847458262,\"deviceSerialNo\":\"JC17\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789347203556893,\"wgs84Lat\":29.789347203556893,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080118\",\"wgs84Lng\":119.79227847458262,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79187423710326,\"deviceSerialNo\":\"JC18\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.790267932778004,\"wgs84Lat\":29.790267932778004,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080119\",\"wgs84Lng\":119.79187423710326,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79205838973274,\"deviceSerialNo\":\"JC19\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.789652808247087,\"wgs84Lat\":29.789652808247087,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080120\",\"wgs84Lng\":119.79205838973274,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79208084737049,\"deviceSerialNo\":\"JC20\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.78918656481612,\"wgs84Lat\":29.78918656481612,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080121\",\"wgs84Lng\":119.79208084737049,\"deviceStatus\":0},{\"deviceType\":1,\"gdLng\":119.79153737253714,\"deviceSerialNo\":\"JC21\",\"level\":{\"red\":30,\"blue\":20,\"yellow\":25},\"gdLat\":29.788649794451683,\"wgs84Lat\":29.788649794451683,\"deviceTypeName\":\"表面位移\",\"vertical\":{\"red\":30,\"blue\":20,\"yellow\":25},\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080122\",\"wgs84Lng\":119.79153737253714,\"deviceStatus\":0},{\"deviceType\":3,\"gdLng\":119.7935252333,\"deviceSerialNo\":\"JR01\",\"level\":{\"red\":550,\"blue\":450,\"yellow\":500},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"渗流压力\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080201\",\"wgs84Lng\":119.7935252333,\"deviceStatus\":0},{\"deviceType\":3,\"gdLng\":118.698284,\"deviceSerialNo\":\"JR02\",\"level\":{\"red\":550,\"blue\":450,\"yellow\":500},\"gdLat\":32.166114,\"wgs84Lat\":32.166114,\"deviceTypeName\":\"渗流压力\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080202\",\"wgs84Lng\":118.698284,\"deviceStatus\":0},{\"deviceType\":2,\"gdLng\":119.7934589467,\"deviceSerialNo\":\"TLYL01\",\"level\":{\"red\":100,\"blue\":50,\"yellow\":80},\"gdLat\":29.7921069433,\"wgs84Lat\":29.7921069433,\"deviceTypeName\":\"降雨量\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080401\",\"wgs84Lng\":119.7934589467,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW1-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080301\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080302\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080303\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79395796,\"deviceSerialNo\":\"NW1-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.793915035,\"wgs84Lat\":29.793915035,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080304\",\"wgs84Lng\":119.79395796,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080305\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080306\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080307\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.7934552333,\"deviceSerialNo\":\"NW2-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.7927588083,\"wgs84Lat\":29.7927588083,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080308\",\"wgs84Lng\":119.7934552333,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080309\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080310\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080311\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.791896694741,\"deviceSerialNo\":\"NW3-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.790342374341158,\"wgs84Lat\":29.790342374341158,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080312\",\"wgs84Lng\":119.791896694741,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-1\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080313\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-2\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080314\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-3\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080315\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0},{\"deviceType\":4,\"gdLng\":119.79201347445726,\"deviceSerialNo\":\"NW4-4\",\"level\":{\"red\":25,\"blue\":15,\"yellow\":20},\"gdLat\":29.789147384595942,\"wgs84Lat\":29.789147384595942,\"deviceTypeName\":\"内部位移\",\"deviceStatusName\":\"在线\",\"deviceId\":\"2022080316\",\"wgs84Lng\":119.79201347445726,\"deviceStatus\":0}]";
        List<IotDeviceInfoVo> iotDeviceInfoVos = JSONArray.parseArray(deviceData, IotDeviceInfoVo.class);
        Map<Integer, List<IotDeviceInfoVo>> deviceMap = iotDeviceInfoVos.stream().collect(Collectors.groupingBy(IotDeviceInfoVo::getDeviceType));

        //查询已存设备数据
        MineIotDevicePo mineIotDevicePo = new MineIotDevicePo();
        mineIotDevicePo.setCompanyCode(CHANGYU_COMPANY_CODE.getCode());
        List<MineIotDevicePo> mineIotSlopeDeviceList = mineIotDeviceService.getMineIotDeviceList(mineIotDevicePo);
        Map<String, List<MineIotDevicePo>> queryDeviceMap = mineIotSlopeDeviceList.stream().collect(Collectors.groupingBy(MineIotDevicePo::getEquipType));

        //设备类型1-边坡表面位移 2-降雨量 3-渗流压力 4-内部位移 5-振动加速度
        List<IotDeviceInfoVo> iotSlopeSurfaceDeviceInfoVos = deviceMap.get(1);
        List<IotDeviceInfoVo> iotRainDeviceInfoVos = deviceMap.get(2);
        List<IotDeviceInfoVo> iotOsPresInfoVos = deviceMap.get(3);
        List<IotDeviceInfoVo> iotSlopeInternalDeviceInfoVos = deviceMap.get(4);
//        List<IotDeviceInfoVo> iotRainDeviceInfoVos = deviceMap.get(5);

        // 处理新增、编辑和删除的数据
        processDeviceData(iotSlopeSurfaceDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770101.getCode()), CHANGYU_COMPANY_CODE.getCode(), CommonMineEnum.HZD770101.getCode());
        processDeviceData(iotRainDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770102.getCode()), CHANGYU_COMPANY_CODE.getCode(), CommonMineEnum.HZD770102.getCode());
        processDeviceData(iotOsPresInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770108.getCode()), CHANGYU_COMPANY_CODE.getCode(), CommonMineEnum.HZD770108.getCode());
        processDeviceData(iotSlopeInternalDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770104.getCode()), CHANGYU_COMPANY_CODE.getCode(), CommonMineEnum.HZD770104.getCode());
//        processDeviceData(iotRainDeviceInfoVos, queryDeviceMap.get(CommonMineEnum.HZD770102.getCode()), NANFANG_COMPANY_CODE.getCode(), CommonMineEnum.HZD770102.getCode());
    }


    /**
     * 处理新增、编辑和删除的数据
     *
     * @param iotDeviceInfoVos       对接的设备信息Vo
     * @param mineIotSlopeDeviceList 当前已存设备信息Po
     */
    private void processDeviceData(List<IotDeviceInfoVo> iotDeviceInfoVos, List<MineIotDevicePo> mineIotSlopeDeviceList, String companyCode, String equipType) {
        if (!CollectionUtils.isEmpty(mineIotSlopeDeviceList) && CollectionUtils.isEmpty(iotDeviceInfoVos)) {
            //如果矿山接口数据为空，当前已存的数据不为空。则删除数据
            List<String> deviceIdList = mineIotSlopeDeviceList.stream().map(MineIotDevicePo::getSourceId).collect(Collectors.toList());
            mineIotDeviceService.batchRemoveMineIotDevice(deviceIdList);
            //同时调用市级接口进行删除
            MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(deviceIdList), restTemplate, "删除设备信息", CITY_SLOPE_DELETE_URL);
            return;
        }
        //查询矿山基础信息
        List<MineBasicDataPo> mineBasicDataList = mineBasicDataService.getMineBasicDataList(new MineBasicDataPo());
        Map<String, MineBasicDataPo> basicDataPoMap = mineBasicDataList.stream().collect(Collectors.toMap(MineBasicDataPo::getCompanyCode, Function.identity()));

        MineBasicDataPo mineBasicDataPo = basicDataPoMap.get(companyCode);
        if (CollectionUtils.isEmpty(mineIotSlopeDeviceList)) {
            //初次新增
            for (IotDeviceInfoVo iotDeviceInfoVo : iotDeviceInfoVos) {
                MineIotDevicePo mineIotDevicePo = new MineIotDevicePo();
                String deviceId = iotDeviceInfoVo.getDeviceId();
                buildDeviceData(mineIotDevicePo, iotDeviceInfoVo, deviceId, mineBasicDataPo, companyCode, equipType);
                //收集需要保存的物联设备与指标信息
                mineIotDeviceService.saveMineIotDevice(mineIotDevicePo);
                //上报市级新增或编辑
                reportCityAddOrUpdate(mineIotDevicePo);
            }
            return;
        }

        //走到这里代表都不为空 需判断新增或删除 或修改的数据
        Map<String, IotDeviceInfoVo> deviceVoMap = iotDeviceInfoVos.stream().collect(Collectors.toMap(IotDeviceInfoVo::getDeviceId, Function.identity(), (key1, key2) -> key2));
        Set<String> deviceSet = new HashSet<>();
        //收集需删除和编辑的数据
        List<MineIotDevicePo> deviceSaveList = new ArrayList<>();
        List<MineIotDevicePo> deviceUpdateList = new ArrayList<>();
        List<String> deviceDeleteList = new ArrayList<>();
        for (MineIotDevicePo mineIotDevicePo : mineIotSlopeDeviceList) {
            String deviceId = mineIotDevicePo.getSourceId();
            if (!deviceVoMap.containsKey(deviceId)) {
                deviceDeleteList.add(mineIotDevicePo.getSourceId());
            } else {
                IotDeviceInfoVo slopeDeviceUpdateVo = deviceVoMap.get(deviceId);
                MineIotDevicePo slopeDeviceUpdatePo = new MineIotDevicePo();
                slopeDeviceUpdatePo.setId(mineIotDevicePo.getId());
                buildDeviceData(slopeDeviceUpdatePo, slopeDeviceUpdateVo, deviceId, mineBasicDataPo, companyCode, equipType);
                //收集需要修改的物联设备与指标信息
                deviceUpdateList.add(slopeDeviceUpdatePo);
            }
            deviceSet.add(deviceId);
        }

        //收集需要新增的数据
        for (IotDeviceInfoVo iotDeviceInfoVo : iotDeviceInfoVos) {
            String deviceId = iotDeviceInfoVo.getDeviceId();
            if (!deviceSet.contains(deviceId)) {
                MineIotDevicePo slopeDeviceAddPo = new MineIotDevicePo();
                buildDeviceData(slopeDeviceAddPo, iotDeviceInfoVo, deviceId, mineBasicDataPo, companyCode, equipType);
                deviceSaveList.add(slopeDeviceAddPo);
            }
        }

        //处理新增编辑和删除的数据
        if (!CollectionUtils.isEmpty(deviceDeleteList)) {
            mineIotDeviceService.batchRemoveMineIotDevice(deviceDeleteList);
            MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(deviceDeleteList), restTemplate, "删除设备信息", CITY_SLOPE_DELETE_URL);
        }
        if (!CollectionUtils.isEmpty(deviceSaveList)) {
            for (MineIotDevicePo mineIotDevicePo : deviceSaveList) {
                mineIotDeviceService.saveMineIotDevice(mineIotDevicePo);
            }
            for (MineIotDevicePo mineIotDevicePo : deviceSaveList) {
                reportCityAddOrUpdate(mineIotDevicePo);
            }
        }
        if (!CollectionUtils.isEmpty(deviceUpdateList)) {
            for (MineIotDevicePo mineIotDevicePo : deviceUpdateList) {
                mineIotDeviceService.updateMineIotDevice(mineIotDevicePo);
            }
            for (MineIotDevicePo mineIotDevicePo : deviceUpdateList) {
                reportCityAddOrUpdate(mineIotDevicePo);
            }
        }
    }

    /**
     * 上报市级的新增和编辑方法
     *
     * @param mineIotDevicePo
     */
    private void reportCityAddOrUpdate(MineIotDevicePo mineIotDevicePo) {
        String stResponse = MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(mineIotDevicePo), restTemplate, "边坡位移监测设备信息", CITY_SLOPE_SAVE_URL);
        if (stResponse == null) {
            return;
        }
        //获取上报后的省级id回填
        JSONObject dataObj = JSONObject.parseObject(stResponse);
        String quotaId = dataObj.getString("equipNo");
        if (quotaId == null) {
            log.error("【" + mineIotDevicePo.getEquipName() + "】" + "边坡位移返回省级源id异常");
            return;
        }
        mineIotDevicePo.setQuotaId(quotaId);
        //回填省级id
        mineIotDeviceService.updateMineIotDevice(mineIotDevicePo);
    }

    /**
     * 构造矿山设备数据
     *
     * @param slopeDevicePo   保存设备实体Po
     * @param slopeDeviceVo   获取企业给的设备对象Vo
     * @param deviceId        企业设备的id
     * @param mineBasicDataPo 矿山基础信息
     * @param companyCode     企业code
     * @param equipType       设备类型编码
     */
    private void buildDeviceData(MineIotDevicePo slopeDevicePo, IotDeviceInfoVo slopeDeviceVo, String deviceId, MineBasicDataPo mineBasicDataPo, String companyCode, String equipType) {
        slopeDevicePo.setCompanyCode(companyCode);
        slopeDevicePo.setEquipName(slopeDeviceVo.getDeviceSerialNo());
        if (companyCode.equals(NANFANG_COMPANY_CODE.getCode())){
            slopeDevicePo.setEquipName(slopeDeviceVo.getTwoLevelName());
        }
        slopeDevicePo.setEquipType(equipType);
        slopeDevicePo.setLatitude(slopeDeviceVo.getWgs84Lat());
        slopeDevicePo.setLongitude(slopeDeviceVo.getWgs84Lng());
        slopeDevicePo.setIsUsed(slopeDeviceVo.getDeviceStatus() == 0 ? 1 : 0);
        slopeDevicePo.setSourceId(deviceId);
        slopeDevicePo.setMineSourceId(mineBasicDataPo.getSourceId());
        slopeDevicePo.setStMineId(mineBasicDataPo.getStSourceId());
        slopeDevicePo.setAltitude(0d);
        slopeDevicePo.setActived(1);
        //按规则生成省厅物联设备编码，截取企业矿山设备末位值当设备位号
        slopeDevicePo.setStSourceId(encodingStSourceId(equipType, companyCode, deviceId));

        //按照目前已对接有设备指标的判断处理
        List<MineIotIndexPo> datas = buildDeviceIotDatas(slopeDeviceVo, equipType, deviceId);
        slopeDevicePo.setDatas(datas);
    }

    private List<MineIotIndexPo> buildDeviceIotDatas(IotDeviceInfoVo slopeDeviceVo, String equipType, String deviceId) {
        List<MineIotIndexPo> datas = new ArrayList<>();
        switch (equipType) {
            case "HZD770101":
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD77010101.getCode(), deviceId, "mm"));
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD77010102.getCode(), deviceId, "mm"));
                datas.add(new MineIotIndexPo(slopeDeviceVo.getVertical(), CommonMineEnum.HZD77010103.getCode(), deviceId, "mm"));
                break;
            case "HZD770102":
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD770102.getCode(), deviceId, ""));
                break;
            case "HZD770104":
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD77010401.getCode(), deviceId, "mm"));
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD77010402.getCode(), deviceId, "mm"));
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD77010403.getCode(), deviceId, "mm"));
                break;
            case "HZD770108":
                datas.add(new MineIotIndexPo(slopeDeviceVo.getLevel(), CommonMineEnum.HZD770108.getCode(), deviceId, ""));
                break;
            default:
                break;
        }
        return datas;
    }

    /**
     * 矿山设备编码规则是根据区划然后加设备类型与设备位号编码，目前这里只到设备类型。
     * 330122100201
     * AABBCCDDDDEE
     * AA-省、BB-市、CC-县、DDDD-矿山、EE设备类型、FF扩展添加
     * EE 为设备类型，具体为: 01 为表面位移:02 为内部位移: 03 为边坡裂缝: 04 为采动应力: 05 为质点速度: 06 为渗透压力: 07 为地下水位高度08 为地下水位速率;09 为降雨量;10 为人员定位;
     * 11 为车辆定位，12 为地下矿山有毒有害气体:13 为地下矿山氧气浓度: 14 为地下矿山风机风速:15 为地下矿山风压: 16 为地下矿山风机启停:17 为地下矿山降雨量: 18 为地下矿山人员定位。
     * <p>
     * FF 为设备位号编码，按照设备位置从左至右，从上至下 的顺序进行编码，保证同类设备位号唯一。
     *
     * @param equipType
     */
    private String encodingStSourceId(String equipType, String companyCode, String deviceId) {
        StringBuilder stSourcePrefix = new StringBuilder("330122");
        String mineDDDD;
        if (companyCode.equals(HONGSHI_COMPANY_CODE.getCode())) {
            mineDDDD = HONGSHI_MINE_CODE.getCode().substring(6);
        } else if (companyCode.equals(NANFANG_COMPANY_CODE.getCode())) {
            mineDDDD = NANFANG_MINE_CODE.getCode().substring(6);
        } else {
            mineDDDD = CHANGYU_MINE_CODE.getCode().substring(6);
        }

        String equipEE = null;
        switch (equipType) {
            case "HZD770101":
                equipEE = "01";
                break;
            case "HZD770104":
                equipEE = "02";
                break;
            case "HZD770105":
                equipEE = "03";
                break;
            case "HZD770106":
                equipEE = "04";
                break;
            case "HZD770107":
                equipEE = "05";
                break;
            case "HZD770108":
                equipEE = "06";
                break;
            case "HZD770110":
                equipEE = "07";
                break;
            case "HZD770111":
                equipEE = "08";
                break;
            case "HZD770102":
                equipEE = "09";
                break;
            case "HZD770103":
                equipEE = "10";
                break;
            case "HZD770109":
                equipEE = "11";
                break;
            case "HZD770201":
                equipEE = "12";
                break;
            case "HZD770207":
                equipEE = "13";
                break;
            case "HZD770203":
                equipEE = "14";
                break;
            case "HZD770204":
                equipEE = "15";
                break;
            case "HZD770205":
                equipEE = "16";
                break;
            case "HZD770206":
                equipEE = "17";
                break;
            case "HZD770202":
                equipEE = "18";
                break;
            default:
                break;
        }

        return MineDeviceCodeUtils.generateNextCode(stSourcePrefix.append(mineDDDD).append(equipEE).toString(), Integer.parseInt(deviceId.substring(deviceId.length() - 2)));
    }
}
